﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ezCloud.Data;
using Microsoft.AspNet.Identity;
using ezCloud.Api.Models;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using ezCloud.Api.Common;
using System.Data.SqlTypes;
using System.Dynamic;
using System.Data.Linq.SqlClient;
using System.Text.RegularExpressions;

namespace ezCloud.Api.Controllers
{
    [RoutePrefix("api/FrontOffice")]
    public class FrontOfficeController : ezController
    {
        public FrontOfficeController()
        {

        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SearchInformation")]
        public object getSearchInformation()
        {
            var user = Common.UserUtitily.getUserProfile();

            List<RoomTypes> roomTypes = context.RoomTypes.Where(rt => rt.HotelId == user.HotelId).ToList();
            List<Rooms> rooms = context.Rooms.Where(r => r.HotelId == user.HotelId).ToList();
            List<RoomPrices> rates = context.RoomPrices.Where(rte => rte.HotelId == user.HotelId).ToList();
            return new
            {
                roomTypes = roomTypes,
                rooms = rooms,
                rates = rates
            };
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("Countries")]
        public List<Countries> getCountries()
        {
            var user = Common.UserUtitily.getUserProfile();

            List<Countries> countries = context.Countries.Where(c => c.CountryId == c.CountryId).ToList();

            return countries;


        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SearchProcess")]
        public object SearchProcess(SearchProcessModel model)
        {
            var user = Common.UserUtitily.getUserProfile();
            var currentHotel = context.Hotels.Where(ht => ht.HotelId == user.HotelId).FirstOrDefault();
            var timeZoneOffset = -currentHotel.TimeZoneOffset / 60;
            TimeSpan offSet = new TimeSpan(timeZoneOffset, 0, 0);
            if (model.From == null)
            {
                model.From = ((DateTime)SqlDateTime.MinValue).Date;
            }
            if (model.To == null)
            {
                model.To = ((DateTime)SqlDateTime.MaxValue).Date;
            }
            if (model.From != null)
            {
                DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.From).Year, ((DateTime)model.From).Month, ((DateTime)model.From).Day, ((DateTime)model.From).Hour, ((DateTime)model.From).Minute, ((DateTime)model.From).Second, offSet).ToOffset(TimeSpan.Zero);
                model.From = fromTemp.Date;
            }
            if (model.To != null)
            {
                DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.To).Year, ((DateTime)model.To).Month, ((DateTime)model.To).Day, ((DateTime)model.To).Hour, ((DateTime)model.To).Minute, ((DateTime)model.To).Second, offSet).ToOffset(TimeSpan.Zero);
                model.To = toTemp.Date;
            }
            
            var reservationRooms = context.ReservationRooms.Where(rr => rr.HotelId == user.HotelId);

            if ( model.ReservationRoomId > 0 )
            {
                //var reservationTemp = reservationRooms.Where(rr => rr.Reservations.ReservationNumber == model.ReservationRoomId).FirstOrDefault();
                reservationRooms = reservationRooms.Where(rr => rr.Reservations.ReservationNumber == model.ReservationRoomId);
                /*
                reservationRooms.Clear();
                if (reservationTemp != null)
                {
                    reservationRooms.AddRange(reservationTemp);
                }*/

            }

            if ( model.RoomTypeId > 0)
            {
                reservationRooms = reservationRooms.Where(rr => rr.RoomTypeId == model.RoomTypeId);
                //reservationRooms.Clear();
                //reservationRooms = reservationTemp;

            }

            if ( model.RoomId > 0)
            {
                reservationRooms = reservationRooms.Where(rr => rr.RoomId == model.RoomId);
                //reservationRooms.Clear();
                //reservationRooms = reservationTemp;
            }

            if ( model.SearchType == "ARRIVAL_LIST")
            {
                //reservationRooms = reservationRooms.Where(rr => ((DateTime)rr.ArrivalDate).Date >= ((DateTime)model.From).Date && ((DateTime)rr.ArrivalDate).Date <= ((DateTime)model.To).Date).ToList();
                //reservationRooms = reservationRooms.Where(rr => ((DateTime)rr.ArrivalDate).Date >= model.From && ((DateTime)rr.ArrivalDate).Date <= model.To).ToList();
                reservationRooms = reservationRooms.Where(
                    rr => new DateTimeOffset(
                        ((DateTime)rr.ArrivalDate).Year,
                        ((DateTime)rr.ArrivalDate).Month,
                        ((DateTime)rr.ArrivalDate).Day,
                        ((DateTime)rr.ArrivalDate).Hour,
                        ((DateTime)rr.ArrivalDate).Minute,
                        ((DateTime)rr.ArrivalDate).Second, offSet).Date >= model.From &&
                        new DateTimeOffset(((DateTime)rr.ArrivalDate).Year,
                                            ((DateTime)rr.ArrivalDate).Month,
                                            ((DateTime)rr.ArrivalDate).Day,
                                            ((DateTime)rr.ArrivalDate).Hour,
                                            ((DateTime)rr.ArrivalDate).Minute,
                                            ((DateTime)rr.ArrivalDate).Second, offSet).Date <= model.To);
                /*
                reservationRooms = reservationRooms.Where(
                    rr => new DateTimeOffset(
                        ((DateTime)rr.ArrivalDate).Year, 
                        ((DateTime)rr.ArrivalDate).Month, 
                        ((DateTime)rr.ArrivalDate).Day, 
                        ((DateTime)rr.ArrivalDate).Hour, 
                        ((DateTime)rr.ArrivalDate).Minute, 
                        ((DateTime)rr.ArrivalDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date >= model.From && 
                        new DateTimeOffset( ((DateTime)rr.ArrivalDate).Year, 
                                            ((DateTime)rr.ArrivalDate).Month, 
                                            ((DateTime)rr.ArrivalDate).Day, 
                                            ((DateTime)rr.ArrivalDate).Hour, 
                                            ((DateTime)rr.ArrivalDate).Minute, 
                                            ((DateTime)rr.ArrivalDate).Second, offSet )
                    .ToOffset(TimeSpan.Zero).Date <= model.To);*/
            }

            if ( (model.SearchType == "DEPARTURE_LIST" || model.SearchType == "CHECKOUT_LIST"))
            {
                //reservationRooms = reservationRooms.Where(rr => ((DateTime)rr.DepartureDate).Date >= ((DateTime)model.From).Date && ((DateTime)rr.DepartureDate).Date <= ((DateTime)model.To).Date).ToList();
                //reservationRooms = reservationRooms.Where(rr => ((DateTime)rr.DepartureDate).Date >= model.From && ((DateTime)rr.DepartureDate).Date <= model.To).ToList();
                reservationRooms = reservationRooms.Where(
                    rr => new DateTimeOffset(
                        ((DateTime)rr.DepartureDate).Year, 
                        ((DateTime)rr.DepartureDate).Month, 
                        ((DateTime)rr.DepartureDate).Day, 
                        ((DateTime)rr.DepartureDate).Hour, 
                        ((DateTime)rr.DepartureDate).Minute, 
                        ((DateTime)rr.DepartureDate).Second, offSet).Date >= model.From && 
                        new DateTimeOffset(((DateTime)rr.DepartureDate).Year, 
                                            ((DateTime)rr.DepartureDate).Month, 
                                            ((DateTime)rr.DepartureDate).Day, 
                                            ((DateTime)rr.DepartureDate).Hour, 
                                            ((DateTime)rr.DepartureDate).Minute, 
                                            ((DateTime)rr.DepartureDate).Second, offSet).Date <= model.To);
                /*
                reservationRooms = reservationRooms.Where(
                    rr => new DateTimeOffset(
                        ((DateTime)rr.DepartureDate).Year,
                        ((DateTime)rr.DepartureDate).Month,
                        ((DateTime)rr.DepartureDate).Day,
                        ((DateTime)rr.DepartureDate).Hour,
                        ((DateTime)rr.DepartureDate).Minute,
                        ((DateTime)rr.DepartureDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date >= model.From && new DateTimeOffset(((DateTime)rr.DepartureDate).Year, ((DateTime)rr.DepartureDate).Month, ((DateTime)rr.DepartureDate).Day, ((DateTime)rr.DepartureDate).Hour, ((DateTime)rr.DepartureDate).Minute, ((DateTime)rr.DepartureDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date <= model.To);*/
            }



            Dictionary<int, object> roomBookingList = new Dictionary<int, object>();

            var reservationRoomsList = reservationRooms
                .Select(rr => new
                {
                    rr.BookingStatus,
                    rr.Adults,
                    rr.Child,
                    rr.ArrivalDate,
                    rr.DepartureDate,
                    rr.Rooms,
                    rr.Reservations,
                    Travellers = new
                    {
                        rr.Travellers.TravellerId,
                        rr.Travellers.Fullname
                    },
                    rr.RoomPrices,
                    rr.RoomTypes,
                    rr.RoomId,
                    rr.RoomTypeId,
                    rr.ReservationRoomId,
                    rr.TravellersList,
                    rr.Foc,
                    rr.DiscountPercentage,
                    rr.DiscountFlat,
                    rr.RoomPriceId,
                    rr.Note,
                    rr.CreatedDate,
                    rr.CreatedUserId,
                    rr.CheckInDate,
                    rr.CheckInUserId,
                    rr.CheckOutDate,
                    rr.CheckOutUserId,
                    rr.CancelDate,
                    rr.CancelUserId,
                    rr.PaymentsList,
                    rr.RoomExtraServicesList,
                    rr.RoomExtraServiceItemsList,
                    rr.Price,
                    rr.Reservations.Company,
                    rr.Reservations.Source,
                    rr.Reservations.Market,
                    rr.IsGroupMaster,
                }).ToList();

            if (model.SearchType == "ARRIVAL_LIST")
            {
                reservationRoomsList = reservationRoomsList.Where(
                    rr => new DateTimeOffset(
                        ((DateTime)rr.ArrivalDate).Year,
                        ((DateTime)rr.ArrivalDate).Month,
                        ((DateTime)rr.ArrivalDate).Day,
                        ((DateTime)rr.ArrivalDate).Hour,
                        ((DateTime)rr.ArrivalDate).Minute,
                        ((DateTime)rr.ArrivalDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date >= model.From &&
                        new DateTimeOffset(((DateTime)rr.ArrivalDate).Year,
                                            ((DateTime)rr.ArrivalDate).Month,
                                            ((DateTime)rr.ArrivalDate).Day,
                                            ((DateTime)rr.ArrivalDate).Hour,
                                            ((DateTime)rr.ArrivalDate).Minute,
                                            ((DateTime)rr.ArrivalDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date <= model.To).ToList();
            }

            if ((model.SearchType == "DEPARTURE_LIST" || model.SearchType == "CHECKOUT_LIST"))
            {
                reservationRoomsList = reservationRoomsList.Where(
                    rr => new DateTimeOffset(
                        ((DateTime)rr.DepartureDate).Year,
                        ((DateTime)rr.DepartureDate).Month,
                        ((DateTime)rr.DepartureDate).Day,
                        ((DateTime)rr.DepartureDate).Hour,
                        ((DateTime)rr.DepartureDate).Minute,
                        ((DateTime)rr.DepartureDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date >= model.From && new DateTimeOffset(((DateTime)rr.DepartureDate).Year, ((DateTime)rr.DepartureDate).Month, ((DateTime)rr.DepartureDate).Day, ((DateTime)rr.DepartureDate).Hour, ((DateTime)rr.DepartureDate).Minute, ((DateTime)rr.DepartureDate).Second, offSet)
                    .ToOffset(TimeSpan.Zero).Date <= model.To).ToList();
            }

            foreach (var rr in reservationRoomsList)
            {
                if (rr.RoomId != null)
                {
                    Rooms room = context.Rooms.Where(rm => rm.HotelId == user.HotelId && rm.RoomId == rr.RoomId).FirstOrDefault();
                    var bookingList = context.ReservationRooms.Where(r => r.HotelId == user.HotelId && r.RoomId == room.RoomId)
                        .Select(r => new
                        {
                            r.BookingStatus,
                            r.Adults,
                            r.Child,
                            r.ArrivalDate,
                            r.DepartureDate,
                            r.ReservationRoomId,
                            Travellers = new
                            {
                                r.Travellers.TravellerId,
                                r.Travellers.Fullname,
                            },
                            Reservations = new
                            {
                                r.Reservations.ReservationNumber,
                            },
                        }).ToList();
                    roomBookingList[room.RoomId] = bookingList;
                }

            }

            var statusColors = context.StatusColors.Where(sc => sc.HotelId == user.HotelId).ToList();

            var reservationInfo = new List<ReservationInfo>();
            
            foreach (var reservation in reservationRoomsList)
            {
                //PaymentInfo infoTemp = new PaymentInfo();
                ReservationInfo infoTemp = new ReservationInfo();
                infoTemp.ReservationRoomId = reservation.ReservationRoomId;
                if (reservation.CreatedUserId != null)
                {
                    var CreatedUserName = context.AspNetUsers.Where(usr => usr.Id == reservation.CreatedUserId).FirstOrDefault().UserName;
                    if (CreatedUserName != null)
                    {
                        infoTemp.CreatedUserName = CreatedUserName;
                    }
                }

                if (reservation.CheckInUserId != null)
                {
                    var CheckInUserName = context.AspNetUsers.Where(usr => usr.Id == reservation.CheckInUserId).FirstOrDefault().UserName;
                    if (CheckInUserName != null)
                    {
                        infoTemp.CheckInUserName = CheckInUserName;
                    }
                }

                if (reservation.CheckOutUserId != null)
                {
                    var CheckOutUserName = context.AspNetUsers.Where(usr => usr.Id == reservation.CheckOutUserId).FirstOrDefault().UserName;
                    if (CheckOutUserName != null)
                    {
                        infoTemp.CheckOutUserName = CheckOutUserName;
                    }
                }
                if (reservation.CancelUserId != null)
                {
                    var CancelUserName = context.AspNetUsers.Where(usr => usr.Id == reservation.CancelUserId).FirstOrDefault().UserName;
                    if (CancelUserName != null)
                    {
                        infoTemp.CancelUserName = CancelUserName;
                    }
                }


                reservationInfo.Add(infoTemp);
            }

            return new
            {
                reservationRooms = reservationRoomsList,
                reservationInfo = reservationInfo,
                statusColors = statusColors,
                roomBookingList = roomBookingList
            };
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SearchReservationProcess")]
        public object SearchReservationProcess(SearchReservationProcessModel model)
        {

            var user = Common.UserUtitily.getUserProfile();
            var currentHotel = context.Hotels.Where(ht => ht.HotelId == user.HotelId).FirstOrDefault();
            var timeZoneOffset = -currentHotel.TimeZoneOffset / 60;
            TimeSpan offSet = new TimeSpan(timeZoneOffset, 0, 0);

            if (model.ArrivalIncluded == false)
            {
                model.ArrivalFrom = ((DateTime)SqlDateTime.MinValue).Date;
                model.ArrivalTo= ((DateTime)SqlDateTime.MaxValue).Date;
            }
            else
            {
                if (model.ArrivalFrom == null)
                {
                    model.ArrivalFrom = ((DateTime)SqlDateTime.MinValue).Date;
                }
                else
                {
                    DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.ArrivalFrom).Year, ((DateTime)model.ArrivalFrom).Month, ((DateTime)model.ArrivalFrom).Day, ((DateTime)model.ArrivalFrom).Hour, ((DateTime)model.ArrivalFrom).Minute, ((DateTime)model.ArrivalFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                    //model.ReservationFrom = fromTemp.Date;
                    DateTime from = (DateTime)fromTemp.Date;
                    from = from.AddMinutes(-currentHotel.TimeZoneOffset);
                    model.ArrivalFrom = from;
                }

                if (model.ArrivalTo == null)
                {
                    model.ArrivalTo = ((DateTime)SqlDateTime.MaxValue).Date;
                }
                else
                {
                    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.ArrivalTo).Year, ((DateTime)model.ArrivalTo).Month, ((DateTime)model.ArrivalTo).Day, ((DateTime)model.ArrivalTo).Hour, ((DateTime)model.ArrivalTo).Minute, ((DateTime)model.ArrivalTo).Second, offSet).ToOffset(TimeSpan.Zero);
                    DateTime to = (DateTime)toTemp.Date;
                    to = to.AddDays(1);
                    model.ArrivalTo = to;
                }
            }

            

            if (model.ReservationFrom == null)
            {
                model.ReservationFrom = ((DateTime)SqlDateTime.MinValue).Date;
            }
            else
            {
                DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.ReservationFrom).Year, ((DateTime)model.ReservationFrom).Month, ((DateTime)model.ReservationFrom).Day, ((DateTime)model.ReservationFrom).Hour, ((DateTime)model.ReservationFrom).Minute, ((DateTime)model.ReservationFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                //model.ReservationFrom = fromTemp.Date;
                DateTime from = (DateTime)fromTemp.Date;
                from = from.AddMinutes(-currentHotel.TimeZoneOffset);
                model.ReservationFrom = from;
            }

            if (model.ReservationTo == null)
            {
                model.ReservationTo = ((DateTime)SqlDateTime.MaxValue).Date;
            }
            else
            {
                DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.ReservationTo).Year, ((DateTime)model.ReservationTo).Month, ((DateTime)model.ReservationTo).Day, ((DateTime)model.ReservationTo).Hour, ((DateTime)model.ReservationTo).Minute, ((DateTime)model.ReservationTo).Second, offSet).ToOffset(TimeSpan.Zero);
                DateTime to = (DateTime)toTemp.Date;
                to = to.AddDays(1);
                model.ReservationTo = to;
            }

            //if (model.ReservationFrom != null)
            //{
            //    DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.ReservationFrom).Year, ((DateTime)model.ReservationFrom).Month, ((DateTime)model.ReservationFrom).Day, ((DateTime)model.ReservationFrom).Hour, ((DateTime)model.ReservationFrom).Minute, ((DateTime)model.ReservationFrom).Second, offSet).ToOffset(TimeSpan.Zero);
            //    model.ReservationFrom = fromTemp.Date;
            //}
            //if (model.ReservationTo != null)
            //{
            //    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.ReservationTo).Year, ((DateTime)model.ReservationTo).Month, ((DateTime)model.ReservationTo).Day, ((DateTime)model.ReservationTo).Hour, ((DateTime)model.ReservationTo).Minute, ((DateTime)model.ReservationTo).Second, offSet).ToOffset(TimeSpan.Zero);
            //    model.ReservationTo = toTemp.Date;
            //}

            var reservationRooms = context.ReservationRooms.Where(rr => rr.HotelId == user.HotelId && rr.ArrivalDate>=model.ArrivalFrom && rr.ArrivalDate<model.ArrivalTo )
                .Select(rr => new
                {
                    rr.BookingStatus,
                    rr.Adults,
                    rr.Child,
                    rr.ArrivalDate,
                    rr.DepartureDate,
                    rr.Rooms,
                    rr.Reservations,
                    Travellers = new
                    {
                        rr.Travellers.TravellerId,
                        rr.Travellers.Fullname
                    },
                    rr.RoomPrices,
                    rr.RoomTypes,
                    rr.RoomId,
                    rr.RoomTypeId,
                    rr.ReservationRoomId,
                    rr.ReservationId,
                    rr.TravellersList,
                    rr.CancelReason,
                    rr.Foc,
                    rr.DiscountPercentage,
                    rr.DiscountFlat,
                    rr.RoomPriceId,
                    rr.Note,
                    rr.CreatedDate,
                    rr.CreatedUserId,
                    rr.CheckInDate,
                    rr.CheckInUserId,
                    rr.CheckOutDate,
                    rr.CheckOutUserId,
                    rr.CancelDate,
                    rr.CancelUserId,
                    rr.PaymentsList,
                    rr.RoomExtraServicesList,
                    rr.RoomExtraServiceItemsList,
                    rr.Price,
                    rr.Reservations.Company,
                    rr.Reservations.Source,
                    rr.Reservations.Market,
                    rr.IsGroupMaster,
                }).ToList();

            if (reservationRooms.Count > 0 && (model.ReservationFrom != null || model.ReservationTo != null))
            {
                //reservationRooms = reservationRooms.Where(rr => new DateTimeOffset(((DateTime)rr.Reservations.CreatedDate).Year, ((DateTime)rr.Reservations.CreatedDate).Month, ((DateTime)rr.Reservations.CreatedDate).Day, ((DateTime)rr.Reservations.CreatedDate).Hour, ((DateTime)rr.Reservations.CreatedDate).Minute, ((DateTime)rr.Reservations.CreatedDate).Second, offSet).ToOffset(TimeSpan.Zero).Date >= model.ReservationFrom && new DateTimeOffset(((DateTime)rr.Reservations.CreatedDate).Year, ((DateTime)rr.Reservations.CreatedDate).Month, ((DateTime)rr.Reservations.CreatedDate).Day, ((DateTime)rr.Reservations.CreatedDate).Hour, ((DateTime)rr.Reservations.CreatedDate).Minute, ((DateTime)rr.Reservations.CreatedDate).Second, offSet).ToOffset(TimeSpan.Zero).Date <= model.ReservationTo).ToList();
                reservationRooms = reservationRooms.Where(rr => rr.CreatedDate >= model.ReservationFrom && rr.CreatedDate < model.ReservationTo).ToList();
            }

            if (reservationRooms.Count > 0 && model.ReservationRoomId > 0)
            {
                //var reservationTemp = reservationRooms.Where(rr => rr.Reservations.ReservationNumber == model.ReservationRoomId).FirstOrDefault();
                var reservationTemp = reservationRooms.Where(rr => rr.Reservations.ReservationNumber == model.ReservationRoomId).ToList();
                reservationRooms.Clear();
                if (reservationTemp != null)
                {
                    reservationRooms.AddRange(reservationTemp);
                }
            }

            if (reservationRooms.Count > 0 && model.RoomTypeId > 0)
            {
                reservationRooms = reservationRooms.Where(rr => rr.RoomTypeId == model.RoomTypeId).ToList();
            }

            if (reservationRooms.Count > 0 && model.RoomId > 0)
            {
                reservationRooms = reservationRooms.Where(rr => rr.RoomId == model.RoomId).ToList();
            }

            /* if (reservationRooms.Count > 0 && model.Type != null)
             {
                 if (model.Type == "NOSHOW")
                 {
                     reservationRooms = reservationRooms.Where(rr => rr.BookingStatus == "BOOKED" && rr.ArrivalDate < DateTime.UtcNow).ToList();
                 }
                 else
                 {
                     reservationRooms = reservationRooms.Where(rr => rr.BookingStatus == model.Type).ToList();
                 }

             }*/

            if (reservationRooms.Count > 0 && model.Type > 0)
            {
                var status = "";
                switch (model.Type)
                {
                    case 1:
                        status = "BOOKED";
                        break;
                    case 2:
                        status = "NOSHOW";
                        break;
                    case 3:
                        status = "CANCELLED";
                        break;
                    case 4:
                        status = "CHECKIN";
                        break;

                }

                if (status == "NOSHOW")
                {
                    reservationRooms = reservationRooms.Where(rr => rr.BookingStatus == "BOOKED" && rr.ArrivalDate < DateTime.UtcNow).ToList();
                }
                else
                {
                    reservationRooms = reservationRooms.Where(rr => rr.BookingStatus == status).ToList();
                }

            }

            if (reservationRooms.Count > 0 && model.GuestName != null)
            {
                /*foreach (object rr in reservationRooms)
                {
                    System.Reflection.PropertyInfo travellers = rr.GetType().GetProperty("Travellers");
                    System.Reflection.PropertyInfo fullName = travellers.GetType().GetProperty("Fullname");
                    string temp = Regex.Replace((string)fullName.GetValue(travellers, null), @"\s+", "" );
                    fullName.SetValue(travellers, temp, null);
                }
                model.GuestName = Regex.Replace(model.GuestName, @"\s+", "");*/
                if (!Regex.IsMatch(model.GuestName, "^[a-zA-Z0-9]+$"))
                {
                    reservationRooms = reservationRooms.Where(rr => rr.Travellers.Fullname.ToLower().Contains(model.GuestName.ToLower())).ToList();
                }
                else
                {
                    Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                    reservationRooms = reservationRooms.Where(rr => Regex.Replace(Utilities.convertToUnSign(rr.Travellers.Fullname).ToLower(), @"\s+", "").Contains(Regex.Replace(model.GuestName, @"\s+", "").ToLower())).ToList();
                }
                //Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                //model.GuestName = rgx.Replace(model.GuestName, "");
                //reservationRooms = reservationRooms.Where(rr => rgx.Replace(rr.Travellers.Fullname,"").ToLower().Contains(model.GuestName.ToLower())).ToList();
            }



            //if (reservationRooms.Count > 0 && model.ArrivalIncluded == true)
            //{
            //    if (model.ArrivalFrom == null)
            //    {
            //        model.ArrivalFrom = (DateTime)SqlDateTime.MinValue;
            //    }

            //    if (model.ArrivalTo == null)
            //    {
            //        model.ArrivalTo = (DateTime)SqlDateTime.MaxValue;
            //    }

            //    if (model.ArrivalFrom != null)
            //    {
            //        DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.ArrivalFrom).Year, ((DateTime)model.ArrivalFrom).Month, ((DateTime)model.ArrivalFrom).Day, ((DateTime)model.ArrivalFrom).Hour, ((DateTime)model.ArrivalFrom).Minute, ((DateTime)model.ArrivalFrom).Second, offSet).ToOffset(TimeSpan.Zero);
            //        model.ArrivalFrom = fromTemp.Date;
            //    }
            //    if (model.ArrivalTo != null)
            //    {
            //        DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.ArrivalTo).Year, ((DateTime)model.ArrivalTo).Month, ((DateTime)model.ArrivalTo).Day, ((DateTime)model.ArrivalTo).Hour, ((DateTime)model.ArrivalTo).Minute, ((DateTime)model.ArrivalTo).Second, offSet).ToOffset(TimeSpan.Zero);
            //        model.ArrivalTo = toTemp.Date;
            //    }
            //    reservationRooms = reservationRooms.Where(rr => new DateTimeOffset(((DateTime)rr.ArrivalDate).Year, ((DateTime)rr.ArrivalDate).Month, ((DateTime)rr.ArrivalDate).Day, ((DateTime)rr.ArrivalDate).Hour, ((DateTime)rr.ArrivalDate).Minute, ((DateTime)rr.ArrivalDate).Second, offSet).ToOffset(TimeSpan.Zero).Date >= model.ArrivalFrom && new DateTimeOffset(((DateTime)rr.ArrivalDate).Year, ((DateTime)rr.ArrivalDate).Month, ((DateTime)rr.ArrivalDate).Day, ((DateTime)rr.ArrivalDate).Hour, ((DateTime)rr.ArrivalDate).Minute, ((DateTime)rr.ArrivalDate).Second, offSet).ToOffset(TimeSpan.Zero).Date <= model.ArrivalTo).ToList();
            //}

            Dictionary<int, object> roomBookingList = new Dictionary<int, object>();
            foreach (var rr in reservationRooms)
            {
                if (rr.RoomId != null)
                {
                    Rooms room = context.Rooms.Where(rm => rm.HotelId == user.HotelId && rm.RoomId == rr.RoomId).FirstOrDefault();
                    if (room != null)
                    {
                        var bookingList = context.ReservationRooms.Where(r => r.HotelId == user.HotelId && r.RoomId == room.RoomId && r.BookingStatus!="CANCELLED" && r.BookingStatus!="CHECKOUT")
                        .Select(r => new
                        {
                            r.BookingStatus,
                            r.Adults,
                            r.Child,
                            r.ArrivalDate,
                            r.DepartureDate,
                            r.ReservationRoomId,
                            Travellers = new
                            {
                                r.Travellers.TravellerId,
                                r.Travellers.Fullname,
                            },
                            Reservations = new
                            {
                                r.Reservations.ReservationNumber,
                            },
                        }).ToList();
                        roomBookingList[room.RoomId] = bookingList;
                    }
                    
                }




            }

            var statusColors = context.StatusColors.Where(sc => sc.HotelId == user.HotelId).ToList();
            var alluser = context.Hotels.Where(ht => ht.HotelId == user.HotelId)
                            .Join(context.HotelOwners, ht => ht.HotelOwnerId, an => an.UserId, (ht, an) => new { Id = an.UserId, UserName = an.FullName })
                            .Union(context.Staffs.Where(st => st.HotelId == user.HotelId).Select(u => new { Id = u.UserId, UserName = u.StaffName })).ToList();

            var reservationInfo = new List<ReservationInfo>();
            foreach (var reservation in reservationRooms)
            {
                //PaymentInfo infoTemp = new PaymentInfo();
                ReservationInfo infoTemp = new ReservationInfo();
                infoTemp.ReservationRoomId = reservation.ReservationRoomId;
                if (reservation.CreatedUserId != null)
                {
                    var CreatedUserName = alluser.Where(usr => usr.Id == reservation.CreatedUserId).FirstOrDefault();
                    if (CreatedUserName != null)
                    {
                        infoTemp.CreatedUserName = CreatedUserName.UserName;
                    }
                }

                if (reservation.CheckInUserId != null)
                {
                    var CheckInUserName = alluser.Where(usr => usr.Id == reservation.CheckInUserId).FirstOrDefault();
                    if (CheckInUserName != null)
                    {
                        infoTemp.CheckInUserName = CheckInUserName.UserName;
                    }
                }

                if (reservation.CheckOutUserId != null)
                {
                    var CheckOutUserName = alluser.Where(usr => usr.Id == reservation.CheckOutUserId).FirstOrDefault();
                    if (CheckOutUserName != null)
                    {
                        infoTemp.CheckOutUserName = CheckOutUserName.UserName;
                    }
                }
                if (reservation.CancelUserId != null)
                {
                    var CancelUserName = alluser.Where(usr => usr.Id == reservation.CancelUserId).FirstOrDefault();
                    if (CancelUserName != null)
                    {
                        infoTemp.CancelUserName = CancelUserName.UserName;
                    }
                }


                reservationInfo.Add(infoTemp);
            }

            return new
            {
                reservationRooms = reservationRooms,
                reservationInfo = reservationInfo,
                statusColors = statusColors,
                roomBookingList = roomBookingList
            };
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SearchGuestListProcess")]
        public object SearchGuestListProcess(SearchGuestListProcessModel model)
        {
            var user = Common.UserUtitily.getUserProfile();
            var currentHotel = context.Hotels.Where(ht => ht.HotelId == user.HotelId).FirstOrDefault();
            var timeZoneOffset = -currentHotel.TimeZoneOffset / 60;
            TimeSpan offSet = new TimeSpan(timeZoneOffset, 0, 0);
            var reservationTravellers = context.ReservationTravellers.Where(rt => rt.Travellers.HotelId == user.HotelId)
                .Select(rt => new
                {
                    ReservationRooms = new
                    {
                        rt.ReservationRooms.Reservations,
                        rt.ReservationRooms.BookingStatus,
                        rt.ReservationRooms.Adults,
                        rt.ReservationRooms.ArrivalDate,
                        rt.ReservationRooms.Child,
                        rt.ReservationRooms.DepartureDate,
                        rt.ReservationRooms.Rooms,
                        rt.ReservationRooms.RoomTypes,
                        rt.ReservationRooms.RoomPrices,
                        rt.ReservationRooms.ReservationRoomId,
                        rt.ReservationRooms.Note,
                        rt.ReservationRooms.CreatedDate,
                        rt.ReservationRooms.CreatedUserId,
                        rt.ReservationRooms.CheckInDate,
                        rt.ReservationRooms.CheckInUserId,
                        rt.ReservationRooms.CheckOutDate,
                        rt.ReservationRooms.CheckOutUserId,
                        rt.ReservationRooms.CancelDate,
                        rt.ReservationRooms.CancelUserId,
                        rt.ReservationRooms.PaymentsList
                    },
                    Travellers = new
                    {
                        rt.Travellers.TravellerId,
                        rt.Travellers.Fullname,
                        rt.Travellers.Mobile,
                        rt.Travellers.IdentityNumber,
                        rt.Travellers.CountryId,
                        rt.Travellers.Birthday,
                        rt.Travellers.Address,
                        rt.Travellers.Email,
                        rt.Travellers.Gender,
                        rt.Travellers.Countries.CountryName,
                        IsDeleted = rt.Travellers.IsDeleted == null ? false : rt.Travellers.IsDeleted.Value,
                        rt.Travellers.DeletedDate,
                        rt.Travellers.DeletedUserId,
                        rt.Travellers.Note,
                    },
                }).ToList();
            foreach (var traveller in reservationTravellers)
            {
                if (traveller.Travellers.CountryId == null)
                {
                    ezCloud.Data.Travellers temp = context.Travellers.Where(tv => tv.HotelId == user.HotelId && tv.TravellerId == traveller.Travellers.TravellerId).FirstOrDefault();
                    var country = context.Countries.First(c => c.CountryCode.ToLower() == "vn");

                    temp.CountryId = country.CountryId;
                    context.SubmitChanges();
                    Common.Elastic.IndexLastAudit(context);
                }
            }

            if (reservationTravellers.Count > 0 && model.ReservationRoomId > 0)
            {
                //var reservationTravellerTemp = reservationTravellers.Where(rr => rr.ReservationRooms.Reservations.ReservationNumber == model.ReservationRoomId).FirstOrDefault();
                var reservationTravellerTemp = reservationTravellers.Where(rr => rr.ReservationRooms.Reservations.ReservationNumber == model.ReservationRoomId).ToList();
                
                if (reservationTravellerTemp != null)
                {
                    reservationTravellers.Clear();
                    foreach (var item in reservationTravellerTemp)
                    {
                        reservationTravellers.Add(item);
                    }
                    
                }

            }

            if (reservationTravellers.Count > 0 && model.GuestName != null)
            {
                if (!Regex.IsMatch(model.GuestName, "^[a-zA-Z0-9]+$"))
                {
                    reservationTravellers = reservationTravellers.Where(rt => rt.Travellers.Fullname.ToLower().Contains(model.GuestName.ToLower())).ToList();
                }
                else
                {
                    Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                    reservationTravellers = reservationTravellers.Where(rr => Regex.Replace(Utilities.convertToUnSign(rr.Travellers.Fullname).ToLower(), @"\s+", "").Contains(Regex.Replace(model.GuestName, @"\s+", "").ToLower())).ToList();
                }
            }

            if (reservationTravellers.Count > 0 && model.IdentityNumber != null)
            {
                reservationTravellers = reservationTravellers.Where(rt => rt.Travellers.IdentityNumber != null && rt.Travellers.IdentityNumber.Contains(model.IdentityNumber)).ToList();
            }

            if (reservationTravellers.Count > 0 && model.Phone != null)
            {
                reservationTravellers = reservationTravellers.Where(rt => rt.Travellers.Mobile != null && rt.Travellers.Mobile.Contains(model.Phone)).ToList();
            }

            if (reservationTravellers.Count > 0 && model.CountryId != 0)
            {
                reservationTravellers = reservationTravellers.Where(rt => rt.Travellers.CountryId == model.CountryId).ToList();
            }

            if (reservationTravellers.Count > 0 && model.ArrivalIncluded == true)
            {
                if (model.ArrivalFrom == null)
                {
                    model.ArrivalFrom = (DateTime)SqlDateTime.MinValue;
                }

                if (model.ArrivalTo == null)
                {
                    model.ArrivalTo = (DateTime)SqlDateTime.MaxValue;
                }

                if (model.ArrivalFrom != null)
                {
                    DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.ArrivalFrom).Year, ((DateTime)model.ArrivalFrom).Month, ((DateTime)model.ArrivalFrom).Day, ((DateTime)model.ArrivalFrom).Hour, ((DateTime)model.ArrivalFrom).Minute, ((DateTime)model.ArrivalFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                    model.ArrivalFrom = fromTemp.Date;
                }
                if (model.ArrivalTo != null)
                {
                    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.ArrivalTo).Year, ((DateTime)model.ArrivalTo).Month, ((DateTime)model.ArrivalTo).Day, ((DateTime)model.ArrivalTo).Hour, ((DateTime)model.ArrivalTo).Minute, ((DateTime)model.ArrivalTo).Second, offSet).ToOffset(TimeSpan.Zero);
                    model.ArrivalTo = toTemp.Date;
                }
                reservationTravellers = reservationTravellers.Where(rt => new DateTimeOffset(((DateTime)rt.ReservationRooms.ArrivalDate).Year, ((DateTime)rt.ReservationRooms.ArrivalDate).Month, ((DateTime)rt.ReservationRooms.ArrivalDate).Day, ((DateTime)rt.ReservationRooms.ArrivalDate).Hour, ((DateTime)rt.ReservationRooms.ArrivalDate).Minute, ((DateTime)rt.ReservationRooms.ArrivalDate).Second, offSet).ToOffset(TimeSpan.Zero).Date >= model.ArrivalFrom && new DateTimeOffset(((DateTime)rt.ReservationRooms.ArrivalDate).Year, ((DateTime)rt.ReservationRooms.ArrivalDate).Month, ((DateTime)rt.ReservationRooms.ArrivalDate).Day, ((DateTime)rt.ReservationRooms.ArrivalDate).Hour, ((DateTime)rt.ReservationRooms.ArrivalDate).Minute, ((DateTime)rt.ReservationRooms.ArrivalDate).Second, offSet).ToOffset(TimeSpan.Zero).Date <= model.ArrivalTo).ToList();
            }

            if (reservationTravellers.Count > 0 && model.DepartureIncluded == true)
            {
                if (model.DepartureFrom == null)
                {
                    model.DepartureFrom = (DateTime)SqlDateTime.MinValue;
                }

                if (model.DepartureTo == null)
                {
                    model.DepartureTo = (DateTime)SqlDateTime.MaxValue;
                }

                if (model.DepartureFrom != null)
                {
                    DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.DepartureFrom).Year, ((DateTime)model.DepartureFrom).Month, ((DateTime)model.DepartureFrom).Day, ((DateTime)model.DepartureFrom).Hour, ((DateTime)model.DepartureFrom).Minute, ((DateTime)model.DepartureFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                    model.DepartureFrom = fromTemp.Date;
                }
                if (model.DepartureTo != null)
                {
                    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.DepartureTo).Year, ((DateTime)model.DepartureTo).Month, ((DateTime)model.DepartureTo).Day, ((DateTime)model.DepartureTo).Hour, ((DateTime)model.DepartureTo).Minute, ((DateTime)model.DepartureTo).Second, offSet).ToOffset(TimeSpan.Zero);
                    model.DepartureTo = toTemp.Date;
                }
                reservationTravellers = reservationTravellers.Where(rt => new DateTimeOffset(((DateTime)rt.ReservationRooms.DepartureDate).Year, ((DateTime)rt.ReservationRooms.DepartureDate).Month, ((DateTime)rt.ReservationRooms.DepartureDate).Day, ((DateTime)rt.ReservationRooms.DepartureDate).Hour, ((DateTime)rt.ReservationRooms.DepartureDate).Minute, ((DateTime)rt.ReservationRooms.DepartureDate).Second, offSet).ToOffset(TimeSpan.Zero).Date >= model.DepartureFrom && new DateTimeOffset(((DateTime)rt.ReservationRooms.DepartureDate).Year, ((DateTime)rt.ReservationRooms.DepartureDate).Month, ((DateTime)rt.ReservationRooms.DepartureDate).Day, ((DateTime)rt.ReservationRooms.DepartureDate).Hour, ((DateTime)rt.ReservationRooms.DepartureDate).Minute, ((DateTime)rt.ReservationRooms.DepartureDate).Second, offSet).ToOffset(TimeSpan.Zero).Date <= model.DepartureTo).ToList();
            }

            return reservationTravellers.GroupBy(rt => rt.Travellers.TravellerId)
                .Select(ReservationRooms => ReservationRooms.ToList())
                .ToList();


            //if (model.ArrivalIncluded)
            //{
            //    if (model.ArrivalFrom == null)
            //    {
            //        model.ArrivalFrom = ((DateTime)SqlDateTime.MinValue).Date;
            //    }

            //    if (model.ArrivalTo == null)
            //    {
            //        model.ArrivalTo = ((DateTime)SqlDateTime.MaxValue).Date;
            //    }

            //    if (model.ArrivalFrom != null)
            //    {
            //        DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.ArrivalFrom).Year, ((DateTime)model.ArrivalFrom).Month, ((DateTime)model.ArrivalFrom).Day, ((DateTime)model.ArrivalFrom).Hour, ((DateTime)model.ArrivalFrom).Minute, ((DateTime)model.ArrivalFrom).Second, offSet).ToOffset(TimeSpan.Zero);
            //        model.ArrivalFrom = fromTemp.Date;
            //    }
            //    if (model.ArrivalTo != null)
            //    {
            //        DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.ArrivalTo).Year, ((DateTime)model.ArrivalTo).Month, ((DateTime)model.ArrivalTo).Day, ((DateTime)model.ArrivalTo).Hour, ((DateTime)model.ArrivalTo).Minute, ((DateTime)model.ArrivalTo).Second, offSet).ToOffset(TimeSpan.Zero);
            //        model.ArrivalTo = toTemp.Date;
            //    }
            //}

            //if (model.DepartureIncluded)
            //{
            //    if (model.DepartureFrom == null)
            //    {
            //        model.DepartureFrom = ((DateTime)SqlDateTime.MinValue).Date;
            //    }

            //    if (model.DepartureTo == null)
            //    {
            //        model.DepartureTo = ((DateTime)SqlDateTime.MaxValue).Date;
            //    }

            //    if (model.DepartureFrom != null)
            //    {
            //        DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.DepartureFrom).Year, ((DateTime)model.DepartureFrom).Month, ((DateTime)model.DepartureFrom).Day, ((DateTime)model.DepartureFrom).Hour, ((DateTime)model.DepartureFrom).Minute, ((DateTime)model.DepartureFrom).Second, offSet).ToOffset(TimeSpan.Zero);
            //        model.DepartureFrom = fromTemp.Date;
            //    }
            //    if (model.DepartureTo != null)
            //    {
            //        DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.DepartureTo).Year, ((DateTime)model.DepartureTo).Month, ((DateTime)model.DepartureTo).Day, ((DateTime)model.DepartureTo).Hour, ((DateTime)model.DepartureTo).Minute, ((DateTime)model.DepartureTo).Second, offSet).ToOffset(TimeSpan.Zero);
            //        model.DepartureTo = toTemp.Date;
            //    }
            //}

            //var travellers = context.Travellers.Where(tv => tv.HotelId == user.HotelId);
        }
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SearchGuestDatabaseProcess")]
        public object SearchGuestDatabaseProcess(SearchGuestDatabaseProcessModel model)
        {
            var user = Common.UserUtitily.getUserProfile();
            var currentHotel = context.Hotels.Where(ht => ht.HotelId == user.HotelId).FirstOrDefault();
            var timeZoneOffset = -currentHotel.TimeZoneOffset / 60;
            TimeSpan offSet = new TimeSpan(timeZoneOffset, 0, 0);

            if (model.CreateDateIncluded == true)
            {

                if (model.createDateFrom == null)
                {
                    model.createDateFrom = (DateTime)SqlDateTime.MinValue;
                }
                else 
                {
                    //DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.createDateFrom).Year, ((DateTime)model.createDateFrom).Month, ((DateTime)model.createDateFrom).Day, ((DateTime)model.createDateFrom).Hour, ((DateTime)model.createDateFrom).Minute, ((DateTime)model.createDateFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                    //model.createDateFrom = fromTemp.Date;

                    DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.createDateFrom).Year, ((DateTime)model.createDateFrom).Month, ((DateTime)model.createDateFrom).Day, ((DateTime)model.createDateFrom).Hour, ((DateTime)model.createDateFrom).Minute, ((DateTime)model.createDateFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                    DateTime from = (DateTime)fromTemp.Date;
                    from = from.AddMinutes(-currentHotel.TimeZoneOffset);
                    model.createDateFrom = from;

                }
                if (model.createDateTo == null)
                {
                    model.createDateTo = (DateTime)SqlDateTime.MaxValue;

                    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.createDateTo).Year, ((DateTime)model.createDateTo).Month, ((DateTime)model.createDateTo).Day, ((DateTime)model.createDateTo).Hour, ((DateTime)model.createDateTo).Minute, ((DateTime)model.createDateTo).Second, offSet).ToOffset(TimeSpan.Zero);
                    model.createDateTo = toTemp.Date;
                }
                else
                {
                    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.createDateTo).Year, ((DateTime)model.createDateTo).Month, ((DateTime)model.createDateTo).Day, ((DateTime)model.createDateTo).Hour, ((DateTime)model.createDateTo).Minute, ((DateTime)model.createDateTo).Second, offSet).ToOffset(TimeSpan.Zero);
                    DateTime to = (DateTime)toTemp.Date;
                    to = to.AddDays(1);
                    model.createDateTo = to;
                }
            }
            else
            {
                model.createDateFrom = (DateTime)SqlDateTime.MinValue;
                model.createDateTo = (DateTime)SqlDateTime.MaxValue;
            }
            

            var travellers = context.Travellers.Where(m => m.HotelId == currentHotel.HotelId ).Select(m=>new {
                m.TravellerId,
                m.Address,
                m.Birthday,
                m.Countries,
                m.CountryId,
                m.CreatedDate,
                m.CreatedUserId,
                m.DeletedDate,
                m.DeletedUserId,
                IsDeleted=m.IsDeleted == null ? false : m.IsDeleted.Value,
                m.Email,
                m.Fullname,
                m.Gender,
                m.HotelId,
                m.IdentityNumber,
                m.Mobile,
                m.ModifiedDate,
                m.ModifiedUserId,
                m.Note
            }).ToList();
            foreach (var traveller in travellers)
            {
                if (traveller.CountryId == null)
                {
                    ezCloud.Data.Travellers temp = context.Travellers.Where(tv => tv.HotelId == user.HotelId && tv.TravellerId == traveller.TravellerId).FirstOrDefault();
                    var country = context.Countries.First(c => c.CountryCode.ToLower() == "vn");
                    temp.CountryId = country.CountryId;
                    context.SubmitChanges();
                    Common.Elastic.IndexLastAudit(context);
                }
            }

            if (travellers.Count > 0 && model.GuestName != null && model.GuestName != "")
            {
                if (!Regex.IsMatch(model.GuestName, "^[a-zA-Z0-9]+$"))
                {
                    travellers = travellers.Where(rt => rt.Fullname.ToLower().Contains(model.GuestName.ToLower())).ToList();
                }
                else
                {
                    Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                    travellers = travellers.Where(rr => Regex.Replace(Utilities.convertToUnSign(rr.Fullname).ToLower(), @"\s+", "").Contains(Regex.Replace(model.GuestName, @"\s+", "").ToLower())).ToList();
                }
            }

            if (travellers.Count > 0 && model.IdentityNumber != null && model.IdentityNumber !="")
            {
                travellers = travellers.Where(rt => rt.IdentityNumber != null && rt.IdentityNumber.Contains(model.IdentityNumber)).ToList();
            }

            if (travellers.Count > 0 && model.Phone != null && model.Phone != "")
            {
                travellers = travellers.Where(rt => rt.Mobile != null && rt.Mobile.Contains(model.Phone)).ToList();
            }

            if (travellers.Count > 0 && model.CountryId != 0)
            {
                travellers = travellers.Where(rt => rt.CountryId == model.CountryId).ToList();
            }

            if (travellers.Count > 0 && model.CreateDateIncluded == true)
            {
                travellers = travellers.Where(t => t.CreatedDate >= model.createDateFrom && t.CreatedDate < model.createDateTo).ToList();

                //if (model.createDateFrom == null)
                //{
                //    model.createDateFrom = (DateTime)SqlDateTime.MinValue;
                //}

                //if (model.createDateTo == null)
                //{
                //    model.createDateTo = (DateTime)SqlDateTime.MaxValue;
                //}

                //if (model.createDateFrom != null)
                //{
                //    DateTimeOffset fromTemp = new DateTimeOffset(((DateTime)model.createDateFrom).Year, ((DateTime)model.createDateFrom).Month, ((DateTime)model.createDateFrom).Day, ((DateTime)model.createDateFrom).Hour, ((DateTime)model.createDateFrom).Minute, ((DateTime)model.createDateFrom).Second, offSet).ToOffset(TimeSpan.Zero);
                //    model.createDateFrom = fromTemp.Date;
                //}
                //if (model.createDateTo != null)
                //{
                //    DateTimeOffset toTemp = new DateTimeOffset(((DateTime)model.createDateTo).Year, ((DateTime)model.createDateTo).Month, ((DateTime)model.createDateTo).Day, ((DateTime)model.createDateTo).Hour, ((DateTime)model.createDateTo).Minute, ((DateTime)model.createDateTo).Second, offSet).ToOffset(TimeSpan.Zero);
                //    model.createDateTo = toTemp.Date;
                //}
                //travellers = travellers.Where(t => 
                //new DateTimeOffset(((DateTime)t.CreatedDate).Year,
                //((DateTime)t.CreatedDate).Month, 
                //((DateTime)t.CreatedDate).Day, 
                //((DateTime)t.CreatedDate).Hour, 
                //((DateTime)t.CreatedDate).Minute, 
                //((DateTime)t.CreatedDate).Second, offSet).ToOffset(TimeSpan.Zero).Date >= model.createDateFrom &&
                //new DateTimeOffset(((DateTime)t.CreatedDate).Year,
                //((DateTime)t.CreatedDate).Month,
                //((DateTime)t.CreatedDate).Day,
                //((DateTime)t.CreatedDate).Hour,
                //((DateTime)t.CreatedDate).Minute,
                //((DateTime)t.CreatedDate).Second, offSet).ToOffset(TimeSpan.Zero).Date <= model.createDateTo)
                //.ToList();
            }
            return travellers.GroupBy(t => t.TravellerId)
                .ToList();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("EditGuestInfo")]
        public async Task<IHttpActionResult> EditGuestInfo(SharerViewModel model)
        {

            var user = Common.UserUtitily.getUserProfile();
            var currentHotel = context.Hotels.Where(h => h.HotelId == user.HotelId).FirstOrDefault();
            ezCloud.Data.Travellers oldTraveller = context.Travellers.Where(tv => tv.TravellerId == model.customer.TravellerId && tv.HotelId == user.HotelId).FirstOrDefault();
            if (oldTraveller != null)
            {
                oldTraveller.Fullname = model.customer.Fullname;
                oldTraveller.Mobile = model.customer.Mobile;
                oldTraveller.Email = model.customer.Email;
                oldTraveller.IdentityNumber = model.customer.IdentityNumber;
                oldTraveller.Birthday = model.customer.Birthday.HasValue ? model.customer.Birthday.Value.AddHours(currentHotel.TimeZoneOffset / 60) : (DateTime)SqlDateTime.MinValue;
                oldTraveller.Address = model.customer.Address;
                oldTraveller.Gender = model.customer.Gender;
                oldTraveller.CountryId = model.customer.CountryId;
                oldTraveller.ModifiedDate = DateTime.UtcNow;
                oldTraveller.ModifiedUserId = user.UserId;
                oldTraveller.Note = model.customer.Note;
                context.SubmitChanges();
                Common.Elastic.IndexLastAudit(context);
                return Ok();
            }

            else
            {
                return BadRequest("ERROR");
            }
        }
        public class Travellers
        {
            public string Fullname { get; set; }
            public Travellers(string fullName)
            {
                Fullname = fullName;
            }
        }
    }
}